/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaces;

import conexion.Operaciones;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jvnet.substance.SubstanceLookAndFeel;

/**
 *
 * @author vikko
 */
public class LogIn extends javax.swing.JFrame {

       
    public LogIn() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.CremeSkin");
        initComponents();
        setLocationRelativeTo(null);        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        etiquetaLogo = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        user_field = new javax.swing.JTextField();
        logIn_btn = new javax.swing.JButton();
        salir_btn = new javax.swing.JButton();
        password_field = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("PC-restore Administrator V1.0");
        setIconImage(getIconImage());
        setResizable(false);

        etiquetaLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/logo.png"))); // NOI18N
        etiquetaLogo.setText("Logo");

        jLabel1.setText("Ingrese su nombre de Usuario y Contraseña");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setText("BIENVENIDO");

        jLabel3.setText("USUARIO:");

        jLabel4.setText("CONTRASEÑA:");

        logIn_btn.setText("Ingresar");
        logIn_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logIn_btnActionPerformed(evt);
            }
        });

        salir_btn.setText("Salir");
        salir_btn.setMaximumSize(new java.awt.Dimension(73, 23));
        salir_btn.setMinimumSize(new java.awt.Dimension(73, 23));
        salir_btn.setPreferredSize(new java.awt.Dimension(73, 23));
        salir_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salir_btnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(167, 167, 167)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(105, 105, 105)
                        .addComponent(jLabel1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(105, 105, 105)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel4)
                                        .addComponent(jLabel3))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(user_field)
                                        .addComponent(password_field, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(salir_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)))
                            .addComponent(logIn_btn))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(15, 15, 15)
                            .addComponent(etiquetaLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(15, 15, 15))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(etiquetaLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jLabel1)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(user_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(password_field))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salir_btn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(logIn_btn))
                .addGap(24, 24, 24))
        );

        getAccessibleContext().setAccessibleName("PC-restore Administrator V 1.0");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void logIn_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logIn_btnActionPerformed
        String usuario=user_field.getText();        
        String contraseña = password_field.getText();       
        Operaciones operacion= new Operaciones();
        String tipoUsuario= operacion.consultaLogIn(usuario, contraseña);  //Si el usuario no existe regresa null
        String nombreUsuario=operacion.buscaNombreEmpleado(usuario);
        System.out.println("tipo usuario: "+tipoUsuario);
        System.out.println("Usuario: "+usuario);        
        try{      
            EntornoUsuario nuevoEntorno= new EntornoUsuario();  
            nuevoEntorno.setName(nombreUsuario);
            if (tipoUsuario.compareTo("Usuario")==0)
                nuevoEntorno.setPrivilegios();
            nuevoEntorno.setVisible(rootPaneCheckingEnabled);            
            nuevoEntorno.setTitle("PC|restore Administrator V1.0                                                                                      Sesión actual de: "+nombreUsuario);            
            nuevoEntorno.setLocationRelativeTo(this);            
            dispose();       
        }catch(NullPointerException e){ 
            user_field.setText("");
            password_field.setText("");
            JOptionPane.showMessageDialog(rootPane, "Usuario y/o constraseña no validos. ", "Error de Log-in", JOptionPane.ERROR_MESSAGE);           
        };
        
    }//GEN-LAST:event_logIn_btnActionPerformed
        
    private void salir_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salir_btnActionPerformed
        dispose();
    }//GEN-LAST:event_salir_btnActionPerformed
    
    
    @Override
    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().
                getImage(ClassLoader.getSystemResource("imagenes/icono_pc.png"));


        return retValue;
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel etiquetaLogo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton logIn_btn;
    private javax.swing.JPasswordField password_field;
    private javax.swing.JButton salir_btn;
    public javax.swing.JTextField user_field;
    // End of variables declaration//GEN-END:variables
}
